package com.tencent.org.service.impl;

import com.tencent.basic.service.impl.BaseServiceImpl;
import com.tencent.org.domain.Department;
import com.tencent.org.mapper.DepartmentMapper;
import com.tencent.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实现类 ： 部门的 Service的 实现类
 */
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Transactional
    @Override
    public void save(Department department) {
        departmentMapper.save(department);

        String dirPath = "";
        // 判断有没有上级部门
        if(department.getParent_id() == null) { // 如果没有选择 或者 选择了又x掉了
            // 拼接自己的dirPath
            dirPath = "/" + department.getId();
        } else { // 选择了上级部门
            // 获取父部门id
            Long parent_id = department.getParent_id();
            // 查出父部门的dirPath
            String parentDirPath = departmentMapper.loadById(parent_id).getDirPath();
            // 拼接自己的dirPath
            dirPath = parentDirPath + "/" + department.getId();
        }
        // 将路径设置进去 再修改
        department.setDirPath(dirPath);
        departmentMapper.update(department);

    }

    @Transactional
    @Override
    public void update(Department department) {
        String dirPath = "";
        // 判断有没有上级部门
        if(department.getParent_id() == null) { // 如果没有选择 或者 选择了又x掉了
            // 拼接自己的dirPath
            dirPath = "/" + department.getId();
        } else { // 选择了上级部门
            // 获取父部门id
            Long parent_id = department.getParent_id();
            // 查出父部门的dirPath
            String parentDirPath = departmentMapper.loadById(parent_id).getDirPath();
            // 拼接自己的dirPath
            dirPath = parentDirPath + "/" + department.getId();
        }
        // 将路径设置进去 再修改
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }


    /**
     * 部门树 - 无限级数
     * @return
     */
    @Override
    public List<Department> queryDeptTree() {
        // 查询出所有的对象
        List<Department> all = departmentMapper.loadAll();

        // 创建一个Map 并将部门对象装进Map中
        Map<Long, Department> map = new HashMap<>();
        for (Department depts : all) {
            map.put(depts.getId(), depts);
        }

        // 创建一个部门树集合
        List<Department> deptTree = new ArrayList<>();
        for (Department dept : all) {
            // 没有上级部门 - 顶级部门 - 直接放进部门树
            if (dept.getParent_id() == null) {
                deptTree.add(dept);
            } else {
                // 有上级部门 - 获取上级部门id
                Long parent_id = dept.getParent_id();
                // 通过Map的key 获取到上级部门
                Department departmentPatent = map.get(parent_id);
                // 把当前部门放进父部门的子集合
                departmentPatent.getChildren().add(dept);
            }
        }
        return deptTree;
    }
}
